package com.moleq.dialog;

import java.math.BigDecimal;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.moleq.base.MGposSender;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.logic.SaleList;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.mgpos.widget.NumberPadForPayment;
import com.moleq.mgpos.widget.NumberPadForPayment.NumberPadListener;
import com.moleq.models.GlobalInfo;
import com.moleq.models.SaleListItem;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class PriceChangeDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private Button btnConfirm;
	private Button btnClose;
	private NumberPadForPayment mNumPad;
	private SaleList saleList;
	private SaleListItem item;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.buttonPriceChangeClose:
//				onCloseWithReturn("");
				dismiss();
				break;
			case R.id.buttonPriceChangeConfirm:
				String strAuth = mNumPad.getBuffer();
				if (strAuth != null && !strAuth.equals("")) {
					//onCloseWithReturn(strAuth);
					
					changePrice(strAuth);
				}

				break;
			}
		}

	};
	
	private NumberPadListener mNumPadListener = new NumberPadListener(){

		@Override
		public void onClick(NumberPadButton button) {
			switch(button){
			case ENTER:
				String strAuth = mNumPad.getBuffer();
				if(strAuth!=null && !strAuth.equals("")){
					changePrice(strAuth);
				}
				break;
			}
			
		}
		
	};
	
	public void changePrice(String price){
		
		int itemIdx = saleList.getSelectedRegularItemIdx();
		if (itemIdx >= 0) {
			item = saleList.getItem(itemIdx);
		}
		double paid = 0;
		BigDecimal b1 = new BigDecimal(price);
		paid = b1.doubleValue();
		if (!price.contains(".")) {
			paid /= 100;
		}
		if (!GlobalInfo.getInstance().isZeroPrice) {
			if (paid == 0) {
				MessageBox.show(mContext, "Cannot change price to $0.");
				return;
			}
		}
		
		if (String.valueOf(paid).length() > 8) {
				MessageBox.show(mContext, "Entered amount is too large.");
				return;
			}
		
		item.setPrice(paid);
//		saleList.mSaleItems.set(itemIdx, item);
		saleList.selectedIndex(itemIdx);
		TransactionManager.getInstance().announceChanges();
		ServiceManager.getInstance().mPosLineItemService.UpdatePriceByNo(item.getPoslineno(),item.getPrice());
		GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
		TransactionManager.getInstance().SaveFreeFuncRpt(GlobalInfo.getInstance().CurrentFreeFuncID, 1, 0);
		MGposSender.getInstance().send("CHANGE "+item.getSenderContent()+" TO "+MoleQUtil.fixNumber(paid));
		dismiss();
	}
	
//	public void onCloseWithReturn(Object obj){
//		mOnResultListener.onReturnValue(DIALOG_INPUT_CHECK_INFO,obj);
//		dismiss();
//	}


	public PriceChangeDialog(Context context, Object base) {
		super(context,R.style.dialog,base);
		constructor(context,base);
	}
	
	public PriceChangeDialog(Context context, int theme,Object base) {
		super(context, theme,base);
		constructor(context,base);
	}
	
	public PriceChangeDialog(Context context) {
		super(context, R.style.dialog, context);
		constructor(context, null);

	}

	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_input_pricechange_info);
		this.setCancelable(false);

		btnConfirm = (Button) findViewById(R.id.buttonPriceChangeConfirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttonPriceChangeClose);
		btnClose.setOnClickListener(mOnClickListener);

		mNumPad = (NumberPadForPayment) findViewById(R.id.authNumPad);
		mNumPad.setNumberPadListener(mNumPadListener);

	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		
		mNumPad.reset();
		mNumPad.requestFocusFromTouch();
		saleList = TransactionManager.getInstance().mSaleListItems;
	}
	
}
